package com.haoshuang.zhouzhoubang.ui.page.mine.contract.fragment

import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.FragmentMyContractChildBinding
import com.haoshuang.zhouzhoubang.model.bean.MyContractBean
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.vertical

/**
 *  
 */
class MyContractChildFragment : BaseVMFragment<MyContractChildViewModel, FragmentMyContractChildBinding>() {

    companion object {
        fun getInstance(status: Int): MyContractChildFragment {
            val fragment = MyContractChildFragment()
            fragment.arguments = Bundle().apply {
                putInt("status", status)
            }
            return fragment
        }
    }

    /**
     * 1.待审核 2.通过 3.拒绝
     */
    private val status by lazy { arguments?.getInt("status") ?: 1 }

    private val data = arrayListOf<MyContractBean>()
    private val adapter = MyContractChildAdapter(data)

    fun refreshData(){
        binding.refreshLayout.autoRefresh()
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.apply{

            refreshLayout.setOnRefreshListener {
                p = 1
                isShowing = false
                vm.getTeamContractList(status, p)
            }

            refreshLayout.setOnLoadMoreListener {
                p++
                isShowing = false
                vm.getTeamContractList(status, p)
            }

            recyclerView.vertical()
                .adapter = adapter

            adapter.setEmptyData()

        }

    }

    override fun initData() {

    }

    override fun lazyLoadData() {
        p = 1
        vm.getTeamContractList(status, p)
    }

    override fun startObserve() {
        vm.getTeamContractListData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, data, adapter, p)
            }
        }
    }

}